package linuxbox.util;

import java.io.*;

/**
 * HTML Output Stream 
 *
 * 1. converts carriage returns to </br>
 */
public class HtmlFilterWriter extends FilterWriter {
    public static final String HTML_LINE_SEPARATOR = "</br>";
    
    public HtmlFilterWriter(Writer aWriter) {
        super(aWriter);
    }
    
    public void write(String str, int off, int len) throws IOException {
        if (str != null) {
            write(str.toCharArray(), off, len);
        }
    }

    public void write(int c) throws IOException {
        if (c == Character.LINE_SEPARATOR) {
            out.write(HTML_LINE_SEPARATOR, 0, HTML_LINE_SEPARATOR.length());
        } else 
        	out.write(c);
    }

    public void write(char[] cbuf, int off, int len) throws IOException {
        if (cbuf != null) {
            for (int i = off; i < len; i++) {
                if (cbuf[i] == Character.LINE_SEPARATOR) {
                    out.write(HTML_LINE_SEPARATOR, 0, HTML_LINE_SEPARATOR.length());
                } else 
                	out.write(cbuf[i]);
            }
        }
    }
}